package cn.biplam.common.client;

import java.rmi.RemoteException;
import java.util.ArrayList;

import cn.biplam.common.bean.sqlengine.Sql;
import cn.biplam.common.interfaces.DatabaseServer;
import cn.biplam.common.tools.RMITOOLS;
import cn.biplam.common.wrapper.ErrorMessage;
import cn.biplam.common.wrapper.sqlresult.SqlResult;

/**
 * 功能		数据库dml和ddl操作
 * @author 	WangSong
 * @create 	2009-3-30 下午05:50:49
 * @version 1.0
 */
public class Database {
	
	private DbResource mDbResource;		//数据库数据源信息
	private DatabaseServer databaseServer;      //数据库服务实例
	private String sUserID;                     //用户ID
	private String sPageID;						//页面ID
	

	/**
	 * 功能：构造方法
	 * @param mDbResource  数据源信息
	 * @param iUserId 用户ID
	 * @param pageId  页面ID
	 * @throws ErrorMessage 
	 */
	public Database(DbResource mDbResource,String sUserID,String sPageID) throws ErrorMessage{
		this.mDbResource = mDbResource;
		this.sUserID = sUserID;
		this.sPageID=sPageID;
		this.databaseServer = (DatabaseServer)RMITOOLS.getInstance("DatabaseServer");
	}
	
	/**
	 * 功能：查询数据库，获得数据集
	 * @param sql
	 * @return	SqlResult
	 * @throws RemoteException
	 * @throws ErrorMessage
	 */
	public SqlResult executeSelect(Sql sql) throws  ErrorMessage{
		return this.executeSelect(sql,SqlResult.SINGLE_PAGE);
	}
	
	/**
	 * 数据库查询
	 * @param sql  
	 * @param iQueryModel 查询开关 Sql.SINGLE_PAGE 单页查询 Sql.MULTI_PAGE 多页查询
	 * @return SqlResult
	 * @throws ErrorMessage 
	 */
	public SqlResult  executeSelect(Sql sql,int iQueryModel) throws ErrorMessage{	
		SqlResult sr = null;
		if(this.databaseServer != null){
			try {
				sr = this.databaseServer.executeSelect(sql,this.mDbResource,iQueryModel,this.sUserID,this.sPageID);
			} catch (RemoteException e) {
				ErrorMessage mErrorMessage = new ErrorMessage(e,"Database.executeSelect failed ");
				throw mErrorMessage;
			}
		}
		return sr;
	}
	
	/**
	 * 数据库查询（限制返回条数）
	 * @param sql  
	 * @param iQueryModel 查询开关 Sql.SINGLE_PAGE 单页查询 Sql.MULTI_PAGE 多页查询
	 * @param resultSize 限制返回条数
	 * @return SqlResult
	 * @throws ErrorMessage 
	 */
	public SqlResult  executeSelectLimit(Sql sql,int resultSize) throws ErrorMessage{	
		SqlResult sr = null;
		if(this.databaseServer != null){
			try {
				sr = this.databaseServer.executeSelect(sql,this.mDbResource,SqlResult.SINGLE_PAGE,this.sUserID,this.sPageID,resultSize);
			} catch (RemoteException e) {
				ErrorMessage mErrorMessage = new ErrorMessage(e);
				throw mErrorMessage;
			}
		}
		return sr;
	}
	
	/**
	 * 数据库查询(分页)
	 * @param sql  查询sql
	 * @param start 开始位置
	 * @param end   结束位置
	 * @return SqlResult
	 * @throws ErrorMessage 
	 */
	public SqlResult  executeSelect(Sql sql,int start,int end) throws ErrorMessage{	
		SqlResult sr = null;
		if(this.databaseServer != null){
			try {
				sr = this.databaseServer.executeSelect(sql,this.mDbResource,Sql.MULTI_PAGE,this.sUserID,this.sPageID,start,end);
			} catch (RemoteException e) {
				ErrorMessage mErrorMessage = new ErrorMessage(e);
				throw mErrorMessage;
			}
		}
		return sr;
	}
	
	/**
	 * 执行 create drop 等不能rollback的 sql的方法
	 * @param sql
	 * @return boolean
	 * @throws ErrorMessage 
	 */
	public boolean executeDDL (Sql sql) throws ErrorMessage{
		boolean boo = true;
		if(this.databaseServer != null){
			try {
				boo = this.databaseServer.executeDDL(sql,this.mDbResource);
			} catch (RemoteException e) {
				ErrorMessage mErrorMessage = new ErrorMessage(e);
				throw mErrorMessage;
			}
		}
		return boo;
	}
	
	/**
	 * 执行 delete  update insert等 可以rollback的 sql的方法
	 * @param sql
	 * @return boolean
	 * @throws ErrorMessage 
	 */
	public boolean executeDML (Sql sql) throws ErrorMessage{
		boolean boo = true;
		if(this.databaseServer != null){
			try {
				boo = this.databaseServer.executeDML(sql,this.mDbResource);
			} catch (RemoteException e) {
				ErrorMessage mErrorMessage = new ErrorMessage(e);
				throw mErrorMessage;
			}
		}
		return boo;
	}
	
	/**
	 * 批量更新
	 * @param sql[]
	 * @return boolean
	 * @throws ErrorMessage 
	 */
	public boolean executeDMLBatch(String...sql) throws ErrorMessage{
		boolean boo = true;
		if(this.databaseServer != null){
			try {
				boo = this.databaseServer.executeDMLBatch(sql,this.mDbResource);
			} catch (RemoteException e) {
				ErrorMessage mErrorMessage = new ErrorMessage(e);
				throw mErrorMessage;
			}
		}
		return boo;	
	}
	
	/**
	 * 批量更新
	 * @param sql[]
	 * @return boolean
	 * @throws ErrorMessage 
	 */
	public boolean executeDMLBatch(ArrayList<String> sql) throws ErrorMessage{
		boolean boo = true;
		if(this.databaseServer != null){
			try {
				boo = this.databaseServer.executeDMLBatch(sql,this.mDbResource);
			} catch (RemoteException e) {
				ErrorMessage mErrorMessage = new ErrorMessage(e);
				throw mErrorMessage;
			}
		}
		return boo;	
	}
}
